global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A22_classification_periods.log, replace name(tabl)

capture noi { 

        * which class of codes (ipc4 vs ipc6XX vs ipc4_pairs) are important for the classification (panel a)

        texdoc init ${tab_dir}/Appendix/Table_A22_classification_periods.tex, replace
        texdoc stlog, nolog


        use ${final_dir}/family_timeseries_full.dta, clear

        qui do ${code_dir}/config/country_list.do
        global countries $countrylist1995
        global lim_countries US DE FR GB JP KR CN
        local del_countries : list global(countries) - global(lim_countries)
        foreach ctry of local del_countries {
	        drop auth_`ctry'
        }
        tempfile small_ts
        save `small_ts', replace

        use ${final_dir}/patlevel_classification_stats.dta, clear

        cap program drop smpl
        program smpl, rclass
                args a
                local N0 "V6"
                local N1 "V6-1997-2011"
                local N2 "V6-until-1997"
                local N3 "V6-from-1998"
                return local name `N`a''
        end

        *restrict to machinery patents
        mmerge docdb_family_id using `small_ts', ukeep(docdb_family_id relevant_field)
        keep if _merge ==3
        *note relevant_field is based on the pats_tfa list. it uses the same identifier but for docdb instead of appln_id
        keep if relevant_field==1

        foreach sample in 0 1 2 3 {
                *row-col, by period
                *yes yes
                count if isauto95_`sample' == 1 & isauto95_0 == 1
                sleep 1000
                texdoc local is`sample'_if0 "\num{`r(N)'}"
                *no-yes
                count if isauto95_`sample' == 1 & isauto95_0 == 0
                sleep 1000
                texdoc local is`sample'_ifnot0 "\num{`r(N)'}"
                *yes-no
                count if isauto95_`sample' == 0 & isauto95_0 == 1
                sleep 1000
                texdoc local isnot`sample'_if0 "\num{`r(N)'}"
                *no-no
                count if isauto95_`sample' == 0 & isauto95_0 == 0
                sleep 1000
                texdoc local isnot`sample'_ifnot0 "\num{`r(N)'}"
                *total-yes
                count if isauto95_`sample' == 1
                sleep 1000
                texdoc local is`sample' "\num{`r(N)'}"
                *total-no
                count if isauto95_`sample' == 0
                sleep 1000
                texdoc local isnot`sample' "\num{`r(N)'}"
        }

        count 
        texdoc local tot "\num{`r(N)'}"

        * output table (via texdoc)
        texdoc local notes "This table classifies all biadic patent families from 1997-2011 as auto95 or not using EPO patents from different time periods. Our baseline measure uses all patents from 1978-2017, while the other measures use patents from the first half of the sample, the second half, or the regression period time."
        sleep 1000
        texdoc stlog close
        qui include ${code_dir}/config/texdoctools.do
        tex `texpreamble'

        /*tex
        \begin{tabular}{@{}lc*{7}{S[table-format = 7]}@{}}
        \toprule
        \multirow{2}{*}{\makecell[l]{Classification \\ periods}} & & \multicolumn{2}{c}{\makecell{First half \\ 1978-1997}} & \multicolumn{2}{c}{\makecell{Second half \\ 1998-2017}} & \multicolumn{2}{c}{\makecell{Regression period \\ 1997-2011}} & \mc{Total} \\
        \cmidrule(lr){3-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8} & & \mc{Yes} & \mc{No} & \mc{Yes} & \mc{No} & \mc{Yes} & \mc{No} & \\
        \midrule
        \multirow{3}{*}{\makecell[l]{Baseline \\ 1978-2017}} & Yes     & `is2_if0'    & `isnot2_if0'    & `is3_if0'    & `isnot3_if0'    & `is1_if0'    & `isnot1_if0'      &  `is0' \\
        & No       & `is2_ifnot0' & `isnot2_ifnot0' & `is3_ifnot0' & `isnot3_ifnot0' & `is1_ifnot0' & `isnot1_ifnot0'   & `isnot0' \\
        & Total   & `is2'        & `isnot2'        & `is3'        & `isnot3'        & `is1'        & `isnot1'          & `tot' \\
        \bottomrule
        \end{tabular}
        tex*/
        tex `texpostamble'
        texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}